package at.fhj.swd11.leitner.ue5;

import java.awt.*;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Vector;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Controller implements ActionListener, ChangeListener{
	
	// Eigenschaften
	private MainGui maingui = null;
	private double cel = 0;
	private double kel = 0;
	private double far = 0;
	private NumberFormat nf = NumberFormat.getInstance();
	private Vector<String> history = new Vector<String>;
	
	// Konstruktor
	public Controller(){
		maingui = new MainGui(this);
		nf.setMaximumFractionDigits(2);
	}
	
	public void actionPerformed(ActionEvent e){
		int answer = JOptionPane.showConfirmDialog(this.maingui, "Wollen sie wirklich beenden", "Apllikation verlassen",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
		if (answer == JOptionPane.YES_OPTION){
			System.exit(0);
		}
		System.exit(0);
	}

	public void stateChanged(ChangeEvent e){
		JSlider js = (JSlider) e.getSource();
		this.cel = js.getValue();
		this.kel = this.cel + 273.5;
		this.far = this.cel * 1.8 + 32;
		this.maingui.getLeftPane().get.tfKelvin().setText(nf.format(this.cel));
		this.maingui.getRightPane().get.tfFahrenheit().setTExt(nf.format(this.far));
		
		if(!js.getValueIsAdjusting()) {
			this.history.add(this.getValues());
			this.maingui.getValuehistory().getListHistory().setListData(this.history);
		}
	}
	
	private String getValues(){
		return "Celsius: "+nf.format(cel)+"Fahrenheit: "+nf.format(far)+"Kelvin: "+nf.format(kel);
	}
}
